<template>
  <div class="app-container">
    <el-row>
      <el-form ref="form" :model="form" inline>
        <el-form-item prop="search">
          <el-input
            v-model="form.search"
            clearable
            style="width:300px"
            prefix-icon="el-icon-search"
            placeholder="输入用户名(username)搜索"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="success" icon="el-icon-search" size="medium" @click="searchByUsername(form)">搜索</el-button>
          <el-button type="warning" icon="el-icon-refresh-left" size="medium" @click="resetForm()">重置</el-button>
        </el-form-item>
      </el-form>
      <el-col :span="17">
        <el-button
          type="primary"
          style="margin-bottom: 20px"
          icon="el-icon-plus"
          size="medium"
        >新增(需到前台进行注册)
        </el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-col>
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>用户名单</span>
          </div>
          <el-table :data="tableData" style="width: 100%" highlight-current-row>
            <el-table-column label="身份证号码" prop="username">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.username }}</span>
              </template>
            </el-table-column>
            <el-table-column label="用户名" prop="nickname">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.nickname }}</span>
              </template>
            </el-table-column>
            <el-table-column label="用户性别" prop="sex">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.sex }}</span>
              </template>
            </el-table-column>
            <el-table-column label="用户身份" prop="roles">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.roles }}</span>
              </template>
            </el-table-column>

            <!-- 车牌信息 -->
            <el-table-column label="车牌信息" prop="carplate" type="expand">
              <template slot-scope="scope">
                <el-popover v-for="(carplate,index) in scope.row.carI " :key="index" trigger="hover" placement="top">
                  <p>车管所名: {{ scope.row.carI[0].dmv }}</p>
                  <p>机动车凭证: {{ carplate.voucher }}</p>
                  <p>品牌种类: {{ carplate.variety }}</p>
                  <p>合格证号: {{ carplate.certificate }}</p>
                  <p>车俩识别代码: {{ carplate.identify }}</p>
                  <div slot="reference" class="name-wrapper">
                    <el-tag size="medium">{{
                      carplate.carplate
                    }}</el-tag>
                  </div>
                </el-popover>
              </template>
            </el-table-column>

            <el-table-column
              fixed="right"
              align="center"
              label="操作"
              width="220"
            >
              <template slot-scope="{ row }">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  size="mini"
                  @click="updateUser(row)"
                >编辑
                </el-button>
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  @click="deleteUser(row)"
                >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
    <!--    调用子组件进行编辑与增加操作-->
    <cuForm
      :dialog-visible="cuDialogVisible"
      :cur-id="curId"
      @close="close"
      @search="search"
    />
  </div>
</template>
<script>
import cuForm from './components/cuForm'
import { deleteUser, getUsers, getUserByUsername } from '@/api/select/userManage'

export default {
  name: 'Users',
  components: { cuForm },
  data() {
    return {
      form: {
        page: 1,
        size: 2,
        search: '3'
      },
      tableData: [],
      multipleSelection: [],
      // cuForm数据
      cuDialogVisible: false,
      curId: null
    }
  },
  created() {
    // 请求后端获取数据
    this.search()
  },
  methods: {
    // 获取角色列表/搜索功能
    search() {
      getUsers().then((res) => {
        this.tableData = res.data.users
      })
    },
    searchByUsername() {
      getUserByUsername(this.form.search).then(response => {
        this.tableData = response.data.results
      }).catch(error => {
        console.log(error)
      })
    },
    // 重置
    resetForm() {
      this.$refs.form.resetFields()
      this.search()
    },

    // 删除User
    deleteUser(row) {
      this.$confirm('此操作将从用户单中移除该用户, 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(row.username)
        deleteUser(row.username).then((res) => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          // 刷新table
          this.search()
        })
      })
    },

    // 关闭子窗口
    close() {
      this.cuDialogVisible = false
      this.curId = null
    },
    // 获得编辑的子窗口
    updateUser(row) {
      // 调用当前更新用户的窗口,并获取当前用户的ID
      this.curId = row.username
      this.cuDialogVisible = true
    }
  }
}
</script>
